package com.ruoyi.zhgd.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.zhgd.annotation.ZhjdDataFilter;
import com.ruoyi.zhgd.domain.NAsphaltData;
import com.ruoyi.zhgd.mapper.NAsphaltCommonMapper;
import com.ruoyi.zhgd.mapper.NAsphaltDataMapper;
import com.ruoyi.zhgd.service.NAsphaltCommonService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 沥青通用业务接口实现
 *
 * @author tll
 * @date 2023-10-12
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltCommonServiceImpl implements NAsphaltCommonService {

    private final NAsphaltCommonMapper baseMapper;

    private final NAsphaltDataMapper nAsphaltDataMapper;

    /**
     * 获取最新一条数据
     *
     * @param params
     * @return
     */
    @Override
    public NAsphaltData getLastData(Map<String, Object> params) {
        QueryWrapper<NAsphaltData> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("stationCode"))) {
            queryWrapper.eq("station_code", params.get("stationCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchType"))) {
            queryWrapper.eq("match_type", params.get("matchType").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchName"))) {
            queryWrapper.eq("match_name", params.get("matchName").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchCode"))) {
            queryWrapper.eq("match_code", params.get("matchCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("day"))) {
            queryWrapper.eq("DATE_FORMAT( discharge_time, '%Y-%m-%d' )", params.get("day").toString());
        }
        queryWrapper.orderByDesc("discharge_time");
        queryWrapper.last(" limit 1 ");
        return nAsphaltDataMapper.selectOne(queryWrapper);
    }

    /**
     * 获取最新数据日期
     *
     * @param params
     * @return
     */
    @Override
    public String getLastDay(Map<String, Object> params) {
        Date date = baseMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }

    /**
     * 获取最新数据月份
     *
     * @param params
     * @return
     */
    @Override
    public String getLastMonth(Map<String, Object> params) {
        Date date = baseMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }

    /**
     * 获取最新数据年份
     *
     * @param params
     * @return
     */
    @Override
    public String getLastYear(Map<String, Object> params) {
        Date date = baseMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }

    /**
     * 获取所有有生产数据的年份
     *
     * @param params
     * @return
     */
    @Override
    public List<String> getAllYear(Map<String, Object> params) {
        return baseMapper.getAllYear(params);
    }
}
